//
//  AlertView1.m
//  u76ho
//
//  Created by m-air-01 on 16/1/7.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "AlertView1.h"
@interface AlertView1 ()
{
    NSString * title;
    NSString * imgNameStr;
    UIImageView * _imageView;

}
@end
@implementation AlertView1
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)text img:(NSString *)imgName
{
    self = [super initWithFrame:frame];
    if (self) {
        title = text;
        imgNameStr = imgName;
        [self addScrenShot];
        [self addAlertView];

    }
    return self;
}
-(void)addScrenShot{
    _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    UIImage * image = [UIImage imageScreenView:RootWindow];
    _imageView.image = image;
    [self addSubview:_imageView];
}
-(void)addAlertView
{
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-SIZETO6W(135)/2, SCREEN_HEIGHT/2- SIZETO6H(100)/2, SIZETO6W(135), SIZETO6H(100))];
    bgView.backgroundColor = UIColorFromRGB(0x333944);
    bgView.alpha = 0.9;
    
    UIImageView * iconImgV = [[UIImageView alloc]initWithFrame:CGRectMake(bgView.width/2-SIZETO6W(35)/2, SIZETO6H(19), SIZETO6W(35), SIZETO6W(35))];
    iconImgV.image = [UIImage imageNamed:imgNameStr];
    [bgView addSubview:iconImgV];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, iconImgV.bottom+SIZETO6H(13), bgView.width, 30)];
    if (iPhone4) {
        titleLab.frame = CGRectMake(0, iconImgV.bottom, bgView.width, 30);
    }
    titleLab.text = title;
    titleLab.textColor = UIColorFromRGB(0xFFFFFF);
    titleLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLab];
    
    [self addSubview:bgView];
    [self animation:bgView addSize:YES];
}
-(void)removeFromSuperviewer
{
    _imageView.hidden = YES;
    [self animation:self addSize:NO];
    
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
}
-(void)animation:(UIView *)view addSize:(BOOL)is
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.2f;
    animation.repeatCount = 1;
    
    if (is) {
        animation.autoreverses = NO;
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        [view.layer addAnimation:animation forKey:@"alertyes"];
    }else{
        animation.autoreverses = YES;
        animation.fromValue = [NSNumber numberWithFloat:1.0f];
        animation.toValue = [NSNumber numberWithFloat:0.0f];
        [view.layer addAnimation:animation forKey:@"alertno"];
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
